import IShanbayWord from './IShanbayWord';
import { setCookie, getCookie } from 'xiao-utils/Cookie';
import Ajax from '@/utils/Ajax';

export default class ShanbayUtils {

    public static decodeFile(files, words: IShanbayWord[]) {
        if (!files || files.length === 0) {
            return;
        }
        const file = files[0];
        const reader = new FileReader();
        const me = this;
        return new Promise(resolve => {
            reader.onload = (e) => {
                const contents = (e.target as any).result;
                me.decodeStr(contents, words);
                resolve();
            };
            reader.readAsText(file);
        });

    }
    public static setLocal(words: IShanbayWord[]) {
        console.log(words.map);
        console.log(typeof words);
        const c = words.map((o) => {
            console.log(o);
            return {
                num: o.num,
                word: o.word,
            };
        });
        console.log(c);
        localStorage.setItem(
            'shanbay-import-from-youdao-words', JSON.stringify(c));
    }
    public static getLocal(words: IShanbayWord[]) {
        const o = localStorage.getItem('shanbay-import-from-youdao-words');
        if (o) {
            for (const oi of JSON.parse(o)) {
                words.push({
                    word: oi.word,
                    msg: '',
                    num: oi.num,
                });
            }
        }
    }

    public static getToken(param: string) {
        return this.decodeUrl(param).access_token;
    }
    public static saveToken(token: string) {
        setCookie('shanbay-token', token);
    }
    public static readToken() {
        return getCookie('shanbay-token');
    }
    public static async openTokenWin(appkey: string) {

        let url = `https://api.shanbay.com/oauth2/authorize/`;
        url += `?client_id=${appkey}`;
        url += `&response_type=token`;
        url += `&redirect_uri=${location.origin}/static/Shanbay.html`;
        window.open(url,
            'newwindow',
            'height=550, width=900, top=0, left=0, toolbar=no,' +
            ' menubar=no, scrollbars=no,resizable=no,location=no, status=no');
    }
    private static decodeUrl(url): any {
        const ret = {};
        const seg = url.split('&');
        const len = seg.length;
        let i = 0;
        let s;
        for (; i < len; i++) {
            if (!seg[i]) { continue; }
            s = seg[i].split('=');
            ret[s[0]] = s[1];
        }
        return ret;
    }
    private static decodeStr(contents: string, words: IShanbayWord[]) {
        const regex =
            new RegExp(/(\d{1,}),\s{0,}(([a-zA-Z]{1,}[ |-]{0,}){1,})(\[|\s)/g);
        let b;
        // tslint:disable-next-line:no-conditional-assignment
        while (b = regex.exec(contents)) {
            if (b.length > 2) {
                const r = {
                    num: b[1],
                    word: b[2].trim(),
                    msg: '',
                };
                words.push(r);
            }
        }
    }
}
